package com.bxl;

import com.bxl.util.ConfigMange;
import com.entity.News;
import com.entity.NewsDetail;
import com.sun.org.apache.bcel.internal.generic.NEW;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.*;

public  class BaseDao {
    /*连接数据库操作对象*/
    static Connection connection=null;
    static PreparedStatement preparedStatement=null;
    static Statement statement=null;
    static ResultSet resultSet=null;



    /*读取外部数据库的配置信息*/
    static final String PATH="E:\\备课笔记\\Kgc备课\\Java全部的课程\\08-Java Web\\database.properties";
    static final String URL=ConfigMange.getConfig(PATH).getValue("url");
    static final String USER_NAME=ConfigMange.getConfig(PATH).getValue("user");
    static final String PASS=ConfigMange.getConfig(PATH).getValue("password");
    static final String DRIVER=ConfigMange.getConfig(PATH).getValue("driver");

  /*  static final String URL=ConfigMange.getConfig().getValue("url");
    static final String USER_NAME=ConfigMange.getConfig().getValue("user");
    static final String PASS=ConfigMange.getConfig().getValue("password");
    static final String DRIVER=ConfigMange.getConfig().getValue("driver");
*/

     /*
      * 连接池：
      *   提前在 某个地方（Tomcat） 存放 多个 数据库连接的对象（connection）
      *   数据源：DataSource：建立与数据库的连接
      *      Connection Connec =DataSource.getConnection();
     * */

    /*
    * 第一步：创建数据库的连接对象
    *   <Resource name="jdbc/news"
              auth="Container"  type="javax.sql.DataSource"  maxActive="100"
              maxIdle="30" maxWait="10000" username="root"  password="bdqn"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://127.0.0.1:3306/kgcnews"/>
    * 第二步：使用JNDI连接数据库
    * */
    public Connection getConnection1(){
        try {
            Context context = new InitialContext();
            String  jndi="java:comp/env/jdbc/news";
            //context.lookup()：查找数据连接对象
            DataSource source= (DataSource)context.lookup(jndi);
           connection =   source.getConnection();

        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }


    //连接数据库
    public static Connection getConnection(){
        try {
          Class.forName(DRIVER);
          connection =  DriverManager.getConnection(URL,USER_NAME,PASS);
       }catch (Exception e){
          e.printStackTrace();
      }
         return connection;
    }




    //增 删 改
    public  int  updateObj(String sql,Object[] obj){
        int rows=0;
        try {
            connection =  BaseDao.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            if(obj !=null){
                for (int i=0;i<obj.length;i++){
                    preparedStatement.setObject(i+1, obj[i]);
                }
                rows= preparedStatement.executeUpdate();
            }
            }catch (Exception e){
                e.printStackTrace();
            }
                 return rows;
    }

    /*练习javabean*/
    public  int  updateObjBean(String sql,Object[] obj){
        int rows=0;
        try {
            connection =  BaseDao.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            if(obj !=null){
                    News news = (News) obj[0];
                    preparedStatement.setObject(1, news.getTitle());
                    preparedStatement.setObject(2, news.getSummary());
                    preparedStatement.setObject(3, news.getContent());
                    preparedStatement.setObject(4, news.getId());
                  rows= preparedStatement.executeUpdate();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return rows;
    }


    //新增
    public  int  InsertObjBean(String sql,Object[] obj){
        int rows=0;
        try {
            connection =  BaseDao.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            if(obj !=null){
                NewsDetail news = (NewsDetail) obj[0];
                preparedStatement.setObject(1, news.getTitle());
                preparedStatement.setObject(2, news.getPicPath());
                rows= preparedStatement.executeUpdate();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return rows;
    }

    //分页
    public  ResultSet selectPageS(String sql,Object[] obj){
        try{
            connection=  BaseDao.getConnection();
            preparedStatement= connection.prepareStatement(sql);
            if(obj !=null && obj.length>0){
                for (int i=0;i<obj.length;i++){
                    preparedStatement.setObject(i+1, obj[i]);
                }
                resultSet= preparedStatement.executeQuery();
            }else {
                resultSet= preparedStatement.executeQuery();
            }
            return resultSet;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return resultSet;
    }

    //通用的查询方法
    public  ResultSet selectObj(String sql,Object[] obj){
            try{
                connection=  BaseDao.getConnection();
                preparedStatement= connection.prepareStatement(sql);
                if(obj !=null && obj.length>0){
                    for (int i=0;i<obj.length;i++){
                        preparedStatement.setObject(i+1, obj[i]);
                    }
                    resultSet= preparedStatement.executeQuery();
                 }else {
                    resultSet= preparedStatement.executeQuery();
                }
                return resultSet;
            }catch (SQLException e){
                  e.printStackTrace();
            }
                return resultSet;
    }



    //关闭连接
    public  void  closeConnection(
            PreparedStatement preparedStatement,
            Statement statement,
            ResultSet resultSet,
            Connection connection
    ){
      try {

          if(resultSet !=null){
              resultSet.close();
          }
          if(preparedStatement !=null){
               preparedStatement.close();
          }
          if(statement !=null){
               statement.close();
          }
          if(connection !=null){
               connection.close();
          }

      }catch (SQLException e){
            e.printStackTrace();
      }

    }
}
